package com.yb.custom.annotation.log.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yb.custom.annotation.log.annotation.ForbidRepeatSubmit;
import com.yb.custom.annotation.log.annotation.LogRecord;
import com.yb.custom.annotation.log.annotation.SignatureVerify;
import com.yb.custom.annotation.log.controller.vo.ResultVO;
import com.yb.custom.annotation.log.entity.OperateLog;
import com.yb.custom.annotation.log.exception.CustomException;
import com.yb.custom.annotation.log.service.OperateLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.time.LocalDateTime;

/**
 * @author yangbiao
 * @since 2022-03-30 15:24:25
 */
@Api(tags = "操作日志接口服务")
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/operateLog")
public class OperateLogController {
    private final OperateLogService operateLogService;

    @SignatureVerify
//    @ForbidRepeatSubmit
    @ApiOperation("post请求测试接口")
    @PostMapping("/postTest")
    public ResultVO<String> postTest(@RequestBody JSONObject jsonObject) {
        return ResultVO.success(JSONObject.toJSONString(jsonObject));
    }

    @SignatureVerify
    @ApiOperation("get请求测试接口")
    @GetMapping("/getTest")
    public ResultVO<String> getTest(
            @ApiParam("参数A")
            @NotBlank(message = "参数A不能为空")
            @Length(max = 20, message = "参数A有误")
            @RequestParam(required = false) String paramA,

            @ApiParam("参数B")
            @NotBlank(message = "参数B不能为空")
            @Length(max = 20, message = "参数B有误")
            @RequestParam(required = false) String paramB) {
        return ResultVO.success(paramA + paramB);
    }

    @LogRecord(operateContent = "日志查询操")
    @ApiOperation("查询分页列表信息")
    @GetMapping("/queryPageInfo")
    public ResultVO<IPage<OperateLog>> queryPageInfo(
            @ApiParam("页码")
            @Min(value = 1, message = "页码有误")
            @RequestParam(defaultValue = "1") Integer page,

            @ApiParam("每页显示条数")
            @Range(min = 1, max = 30, message = "每页显示条数需要在1到30的范围")
            @RequestParam(defaultValue = "10") Integer size,

            @ApiParam("日志类型(1-登录|2-操作)")
            @Range(min = 1, max = 2, message = "未知的日志类型")
            @RequestParam(required = false) Integer logType,

            @ApiParam("请求类型(GET|POST)")
            @Pattern(regexp = "^$|^GET|POST$", message = "请求类型有误")
            @RequestParam(required = false) String requestType,

            @ApiParam("用户机构")
            @Length(max = 20, message = "用户机构有误")
            @RequestParam(required = false) String userOrganization,

            @ApiParam("用户部门")
            @Length(max = 20, message = "用户部门有误")
            @RequestParam(required = false) String userDepartment,

            @ApiParam("用户职位")
            @Length(max = 20, message = "用户职位有误")
            @RequestParam(required = false) String userPosition,

            @ApiParam("用户姓名")
            @Length(max = 20, message = "用户姓名有误")
            @RequestParam(required = false) String userFullName,

            @ApiParam("用户角色")
            @Length(max = 20, message = "用户角色有误")
            @RequestParam(required = false) String userRole,

            @ApiParam("操作用户名")
            @Length(max = 50, message = "操作用户名有误")
            @RequestParam(required = false) String operateUsername,

            @ApiParam("操作者IP")
            @Length(max = 20, message = "操作者IP有误")
            @RequestParam(required = false) String operateIp,

            @ApiParam("操作内容")
            @Length(max = 20, message = "操作内容有误")
            @RequestParam(required = false) String operateContent,

            @ApiParam("操作时间-开始")
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
            @RequestParam(required = false) LocalDateTime operateTimeStart,

            @ApiParam("操作时间-结束")
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
            @RequestParam(required = false) LocalDateTime operateTimeEnd) {
        IPage<OperateLog> result = operateLogService.queryPageInfo(page, size, logType, requestType,
                userOrganization, userDepartment, userPosition, userFullName, userRole, operateUsername,
                operateIp, operateContent, operateTimeStart, operateTimeEnd);
        return ResultVO.success(result);
    }

}

